package com.example.ddcoursequery;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyViewPagerAdapter extends PagerAdapter {

    private List<List<AppCourse>> viewList;
    private List<String> titleList;
    private LayoutInflater layoutInflater;
    private Context context;

    public MyViewPagerAdapter(Context context, List<List<AppCourse>> viewList, List<String> titleList) {
        this.viewList = viewList;
        this.context = context;
        this.titleList = titleList;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public void addView(List<AppCourse> appCourse, String title) {
        titleList.add(title);
        viewList.add(appCourse);
        notifyDataSetChanged();
    }

    //返回所有视图的数量
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return viewList.size();
    }

    //判断视图是否由对象产生
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    //实例化页面
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        View view = layoutInflater.inflate(R.layout.page_item, null);
        ListView listView = (ListView) view.findViewById(R.id.textViewTab);
        List<AppCourse> appCourseList = viewList.get(position);
        String[] tmp = new String[appCourseList.size()];
        int i = 0;
        for (AppCourse a : appCourseList) {
            tmp[i] = a.toString();
            i++;
        }
        ArrayAdapter arrayAdapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, tmp);
        listView.setAdapter(arrayAdapter);
        container.addView(view);
        return view;
    }

    //删除页面
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub

    }

    @Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        return titleList.get(position);
    }

}

